package com.czy.owner.ui.archive;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.czy.owner.R;
import com.czy.owner.api.UploadFrameNumberApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.FrameNumberModel;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseFragment;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.widget.takephoto.CameraSurfaceView;
import com.iflytek.thirdparty.A;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by hedepu on 2018-02-27
 */

public class TakeFrameFrament extends BaseFragment {

    @BindView(R.id.cameraSurfaceView)
    CameraSurfaceView cameraSurfaceView;
    @BindView(R.id.takePic)
    Button takePic;
    public final static int SCANNING_FRAME_RESULTCODE = 10011;
    public final static int SCANNING_FRAME_REQUESTCODE = 10012;
    public final static String SCANNING_FRAME_INTENT = "FrameNumberModel";

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_take_frame_number;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        cameraSurfaceView.setOnTakeSuccessPath(new CameraSurfaceView.OnTakeSuccessPath() {
            @Override
            public void onPath(String path) {

                getVinNumber(path);
            }

            @Override
            public void onStart() {
                mLoadView.ShowLoadView();
            }

            @Override
            public void onFail() {
                mLoadView.CancleLoadView();
            }
        });
    }

    @Override
    protected void updateViews() {

    }

    @OnClick({R.id.takePic})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.takePic:
                cameraSurfaceView.takePicture();
                break;
        }
    }

    private void getVinNumber(String path) {

        UploadFrameNumberApi uploadFrameNumberApi = new UploadFrameNumberApi(new HttpOnNextListener<FrameNumberModel>() {

            @Override
            public void onNext(FrameNumberModel frameNumberModel) {
                mLoadView.CancleLoadView();
                if (!StringUtils.isEmpty(frameNumberModel.getVin())) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(SCANNING_FRAME_INTENT, frameNumberModel);
                    intent.putExtras(bundle);
                    getActivity().setResult(SCANNING_FRAME_RESULTCODE, intent);
                    getActivity().finish();

                } else {
                    PhoneUtils.ShowToastMessage(getActivity(), "未识别出车架号");
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mLoadView.CancleLoadView();
            }
        }, (RxAppCompatActivity) getActivity());
        uploadFrameNumberApi.setSession(UserHelper.getInstance().getSession(getActivity()));
        uploadFrameNumberApi.setPath(path);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(uploadFrameNumberApi);
    }
}
